package com.aaa.controller.front;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aaa.controller.BaseController;
import com.aaa.dao.sas.TesterResultSasDao;
import com.aaa.entity.*;
import com.aaa.entity.fpa.QuestionFpa;
import com.aaa.entity.fpa.TestResultFpa;
import com.aaa.entity.fpa.TesterFpa;
import com.aaa.entity.fpa.TesterVo;
import com.aaa.entity.mht.QuestionMht;
import com.aaa.entity.mht.TestResultMht;
import com.aaa.entity.mht.TesterMht;
import com.aaa.entity.sas.QuestionSas;
import com.aaa.entity.sas.TestResultSas;
import com.aaa.entity.sas.TesterSas;
import com.aaa.entity.scl.QuestionScl;
import com.aaa.entity.scl.TestResultScl;
import com.aaa.entity.scl.TesterScl;
import com.aaa.entity.sds.QuestionSds;
import com.aaa.entity.sds.TestResultSds;
import com.aaa.entity.sds.TesterSds;
import com.aaa.service.TestPlanService;
import com.aaa.service.fpa.QuestionFpaService;
import com.aaa.service.fpa.TestResultFpaService;
import com.aaa.service.fpa.TesterFpaService;
import com.aaa.service.fpa.ViewTestResultFpaService;
import com.aaa.service.mht.QuestionMhtService;
import com.aaa.service.mht.TestResultMhtService;
import com.aaa.service.mht.TesterMhtService;
import com.aaa.service.sas.*;
import com.aaa.service.scl.QuestionSclService;
import com.aaa.service.scl.TestResultSclService;
import com.aaa.service.scl.TesterSclService;
import com.aaa.service.sds.QuestionSdsService;
import com.aaa.service.sds.TestResultSdsService;
import com.aaa.service.sds.TesterSdsService;
import com.aaa.service.sds.ViewTestResultSdsService;
import com.aaa.utils.MyConstants;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author ：李彬
 * @date ：2022/9/2
 * @description：前端请求的控制器
 * @modified
 * @version:
 */
@RestController
@RequestMapping("front")
public class FrontController extends BaseController {
    @Autowired
      TestPlanService testPlanService;
    @Autowired
    QuestionFpaService questionFpaService;

    @Autowired
    TesterFpaService testerFpaService;

    @Autowired
    TestResultFpaService testResultFpaService;

    @Autowired
    ViewTestResultFpaService viewTestResultFpaService;



    @Autowired
    QuestionSclService questionSclService;

    @Autowired
    TesterSclService testerSclService;

    @Autowired
    TestResultSclService testResultSclService;



    @Autowired
    TesterSasService testerSasService;
    @Autowired
    QuestionSasService questionSasService;
    @Autowired
    TestPlanSasServices testPlanSasServices;
    @Autowired
    TestResultSasService testResultSasService;
    @Autowired
    ViewTestResultSasService viewTestResultSasService;

    @Autowired
    QuestionSdsService questionSdsService;

    @Autowired
    TesterSdsService testerSdsService;

    @Autowired
    TestResultSdsService testResultSdsService;
    @Autowired
    ViewTestResultSdsService viewTestResultSdsService;


    @Autowired
    QuestionMhtService questionMhtService;

    @Autowired
    TesterMhtService testerMhtService;

    @Autowired
    TestResultMhtService testResultMhtService;


    @RequestMapping("toShowQuestion.do")
    public ModelAndView toShowQuestion(ModelAndView modelAndView){
        modelAndView.setViewName("front/testQuestion");
        return modelAndView;
    }

    /**
     * @param
     * @return com.aaa.entity.Result
     * @create by: 李彬
     * @description:1、校验测试手机号是否已经测试过 2、校验邀请码是否合法
     * 2.1、如果没有对应的测试计划，非法
     * 2.3、过期的，或者未开始的
     * 3、根据邀请码获取所有问题列表
     * @create 2022/9/2
     * @param
     * @return com.aaa.entity.Result
     */
    @PostMapping("checkTest.do")
    public Result checkTest(@RequestBody TesterVo testerVo, HttpSession session) {
        //根据测试邀请码判断当前所属的测试类型，去数据库的视图view_test_plan
        TestPlan testPlan = testPlanService.queryByTestCode(testerVo.getTestCode());
        //校验邀请码是否合法
        if (ObjectUtil.isNotEmpty(testPlan)) {
            Date testBegin = testPlan.getTestBegin();
            Date testEnd = testPlan.getTestEnd();
            Date now = new Date();
            if (now.before(testBegin)) {
                return fail("测试未开始");
            }
            if (now.after(testEnd)) {
                return fail("测试已经结束，你来晚啦");
            }
            //正常情况,获取测试类型
            String type = testPlan.getType();
            testerVo.setType(type);
            testerVo.setTestCode(testPlan.getTestCode());
            testerVo.setTestName(testPlan.getTestName());
            testerVo.setTestPlanId(testPlan.getId());
            //默认手机号没有重复，false
            Boolean flag = false;

            List<Question> questionList = new ArrayList<>();

            switch (type) {
                case MyConstants.FPA:
                    TesterFpa testerFpa = testerFpaService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if (ObjectUtil.isNotEmpty(testerFpa)) {
                        flag = true;
                    } else {
                        //开始测试，进入到显示所有题目的页面
                        //将所有的测试题放入到session中
                        List<QuestionFpa> questionFpaList = questionFpaService.queryAll(PageSortEntity.noPage(), null);
                        //将 List<QuestionFpa>  转换成List<Question>
                        questionList = BeanUtil.copyToList(questionFpaList, Question.class);
                    }
                    break;
                case MyConstants.SAS:
                    TesterSas testerSas = testerSasService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if (ObjectUtil.isNotEmpty(testerSas)) {
                        flag = true;
                    } else {
                        //开始测试，进入到显示所有题目的页面
                        //将所有的测试题放入到session中
                        List<QuestionSas> questionSasList = questionSasService.queryAll(PageSortEntity.noPage(), null);
                        //将 List<QuestionSas>  转换成List<Question>
                        questionList = BeanUtil.copyToList(questionSasList, Question.class);
                    }
                    break;
                case MyConstants.SDS:
                    TesterSds testerSds = testerSdsService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if (ObjectUtil.isNotEmpty(testerSds)) {
                        flag = true;
                    } else {
                        //开始测试，进入发哦显示所有题目的页面
                        //将所有的测试题放入到session中
                        PageSortEntity pageSortEntity = new PageSortEntity();
                        pageSortEntity.setPage(1);
                        pageSortEntity.setLimit(Integer.MAX_VALUE);
                        List<QuestionSds> questionQuestionSdsList = questionSdsService.queryAll(pageSortEntity, null);
                        //将List<Sds> 转换成 List<QuestionSds>
                        questionList = BeanUtil.copyToList(questionQuestionSdsList, Question.class);
                        session.setAttribute("tester", testerVo);
                        session.setAttribute("questionList", questionList);
                        return success();
                    }
                    break;
                case MyConstants.MHT:
                    TesterMht testerMht = testerMhtService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if (ObjectUtil.isNotEmpty(testerMht)) {
                        flag = true;
                    } else {
                        //开始测试，进入到显示所有题目的页面
                        //将所有的测试题放入到session中
                        List<QuestionMht> questionMhtList = questionMhtService.queryAll(PageSortEntity.noPage(), null);
                        //将 List<QuestionFpa>  转换成List<Question>
                        questionList = BeanUtil.copyToList(questionMhtList, Question.class);
                    }
                    break;
                case MyConstants.SCL:
                    TesterScl testerScl = testerSclService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if (ObjectUtil.isNotEmpty(testerScl)) {
                        flag = true;
                    } else {
                        //开始测试，进入到显示所有题目的页面  将所有的测试题放入到session中
                        List<QuestionScl> questionSclList = questionSclService.queryAll(PageSortEntity.noPage(), null);
                        questionList = BeanUtil.copyToList(questionSclList, Question.class);
                    }
                    break;
                default:
                    return fail("不识别的测试类型");
            }
            if (flag) {
                return fail("手机号已经做过测试");
            }

            //保存tester到session中
            session.setAttribute("tester", testerVo);
            //保存questionList到session中
            session.setAttribute("questionList", questionList);
            return success();
        } else {
            //如果没有对应的测试计划，非法
            return fail("测试邀请码无效");
        }
    }


    @PostMapping("saveBatchTestResult.do")
    public Result saveBatchTestResult(@RequestBody List<TestResultVo> testResultVoList, HttpSession session) {
        TesterVo tester = (TesterVo) session.getAttribute("tester");
        String type = tester.getType();
        switch (type) {
            case MyConstants.FPA:
                //1、根据测试类型进行保存测试者的信息  TesterFpa
                TesterFpa testerFpa = new TesterFpa();
                BeanUtil.copyProperties(tester, testerFpa);
                testerFpaService.insert(testerFpa);
                //testerFpa中自动包含主键信息
                //2、根据测试类型进行保存答案
                /*System.out.println(testResultVoList.size());*/
                List<TestResultFpa> testResultFpaList = BeanUtil.copyToList(testResultVoList, TestResultFpa.class);
                //填充测试者的id
                for (TestResultFpa testResultFpa : testResultFpaList) {
                    testResultFpa.setTesterId(testerFpa.getId());
                }
                testResultFpaService.insertBatch(testResultFpaList);

                Integer id = testerFpa.getId();
                Object s = viewTestResultFpaService.selecById(id);

                /*Map map=new HashMap<>();
                map.put("s",s);*/
                return success(s);
            case MyConstants.SAS:
                //1、根据测试类型进行保存测试者的信息  TesterFpa
                TesterSas testerSas = new TesterSas();
                BeanUtil.copyProperties(tester, testerSas);
                testerSasService.insert(testerSas);
                //testerFpa中自动包含主键信息
                //2、根据测试类型进行保存答案
                List<TestResultSas> testResultSasList = BeanUtil.copyToList(testResultVoList, TestResultSas.class);
                //填充测试者的id
                for (TestResultSas testResultSas : testResultSasList) {
                    testResultSas.setTesterId(testerSas.getId());
                }
                testResultSasService.insertBatch(testResultSasList);

                Integer sasId = testerSas.getId();
                Object ss = viewTestResultSasService.selectById(sasId);
                return success(ss);
            case MyConstants.SDS:
                //1、根据测试类型保存测试者的信息  TesterSds
                TesterSds testerSds = new TesterSds();
                BeanUtil.copyProperties(tester, testerSds);
                testerSdsService.insert(testerSds);
                //testerSds中自动包含主键信息
                //2、根据测试类型保存答案
                /*System.out.println(testResultVosList.size());*/
                List<TestResultSds> testResultSdsList = BeanUtil.copyToList(testResultVoList, TestResultSds.class);
                //现在题少，就三十几道题，可以遍历，要是几万条，就不能遍历了，太费劲了
                for (TestResultSds testResultSds : testResultSdsList) {
                    //填充测试者的Id (testerSds.getId()获取测试者的id)
                    testResultSds.setTesterId(testerSds.getId());
                }
                testResultSdsService.insertBatch(testResultSdsList);
                Integer sdsId = testerSds.getId();
                Object s1 = viewTestResultSdsService.selectById(sdsId);

                /*Map map=new HashMap<>();
                map.put("s",s);*/
                return success(s1);
            case MyConstants.MHT:
                //1、根据测试类型进行保存测试者的信息  TesterFpa
                TesterMht testerMht = new TesterMht();
                //添加tester属性
                BeanUtil.copyProperties(tester, testerMht);
                //添加测试者并且获取测试者id
                TesterMht insert = testerMhtService.insert(testerMht);
                session.setAttribute("testerId", insert.getId());
                //testerFpa中自动包含主键信息
                //2、根据测试类型进行保存答案
                /*System.out.println(testResultVoList.size());*/
                List<TestResultMht> testResultMhts = BeanUtil.copyToList(testResultVoList, TestResultMht.class);
                //填充测试者的id
                for (TestResultMht testResultMht : testResultMhts) {
                    testResultMht.setTesterId(testerMht.getId());
                }
                testResultMhtService.insertBatch(testResultMhts);

                Object urlhref = "mht.jsp";
                return success(urlhref);
            case MyConstants.SCL:
                //1、根据测试类型进行保存测试者的信息  TesterFpa
                TesterScl testerScl =new TesterScl();
                BeanUtil.copyProperties(tester,testerScl);
                TesterScl insert1 = testerSclService.insert(testerScl);
                session.setAttribute("testerId",insert1.getId());
                //testerFpa中自动包含主键信息
                //2、根据测试类型进行保存答案
                /*System.out.println(testResultVoList.size());*/
                List<TestResultScl> testResultSclList = BeanUtil.copyToList(testResultVoList, TestResultScl.class);
                //填充测试者的id
                for (TestResultScl testResultScl : testResultSclList) {
                    testResultScl.setTesterId(testerScl.getId());
                }
                testResultSclService.insertBatch(testResultSclList);
                Object scljsp="scl.jsp";
                return success(scljsp);
            default:
                return fail("不识别的测试类型");
        }
    }



}
